package com.ztmg.water.service.apartment.impl;

import com.ztmg.common.util.DateUtil;
import com.ztmg.water.dto.apartment.ApartmentFloorDTO;
import com.ztmg.water.mapper.apartment.ApartmentFloorMapper;
import com.ztmg.water.model.apartment.ApartmentRooms;
import com.ztmg.water.vo.apartment.ApartmentRoomsVO;
import com.ztmg.water.dto.apartment.ApartmentRoomsDTO;
import com.ztmg.water.dto.apartment.ApartmentRoomsPageDTO;
import com.ztmg.common.util.resultUtils.JsonResult;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ztmg.common.constant.ErrorConst;
import org.springframework.beans.BeanUtils;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import com.ztmg.water.service.apartment.ApartmentRoomsService;
import com.ztmg.water.mapper.apartment.ApartmentRoomsMapper;import com.ztmg.water.model.apartment.ApartmentRoomsExample;

@Service
public class ApartmentRoomsServiceImpl implements ApartmentRoomsService{

    @Resource
    private ApartmentRoomsMapper apartmentRoomsMapper;
    @Resource
    private ApartmentFloorMapper apartmentFloorMapper;
    @Override
    public JsonResult create(ApartmentRoomsDTO dto) {
        if(null != dto){
            ApartmentRooms bean = new ApartmentRooms();
            BeanUtils.copyProperties(dto,bean);
            bean.setCreateTime(new Date());
            //后续完善
            bean.setCreater("");
            apartmentRoomsMapper.insert(bean);
            //修改当前公寓楼层房间数量
            ApartmentFloorDTO paramsObj = new ApartmentFloorDTO();
            paramsObj.setId(bean.getApartmentId());
            paramsObj.setFloorRooms(1);
            paramsObj.setModifier("");
            paramsObj.setModifyTime(DateUtil.parseDateToStr(new Date(),"yyyy-MM-dd HH:mm:ss"));
            apartmentFloorMapper.addRoomNumsById(paramsObj);
            return JsonResult.success();
        }else {
            return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
        }
    }

    @Override
    public JsonResult update(ApartmentRoomsDTO dto) {
        if(null != dto && null != dto.getId()){
            ApartmentRooms bean = new ApartmentRooms();
            BeanUtils.copyProperties(dto,bean);
            bean.setModifyTime(new Date());
            //后续完善
            bean.setModifier("");
            apartmentRoomsMapper.updateByPrimaryKeySelective(bean);
            return JsonResult.success();
        }else {
            return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
        }
    }

    @Override
    public JsonResult<ApartmentRoomsVO> selectById(Integer id) {
        if(null != id){
            ApartmentRooms bean = apartmentRoomsMapper.selectByPrimaryKey(id);
            ApartmentRoomsVO vo = new ApartmentRoomsVO();
            if(null != bean){
                BeanUtils.copyProperties(bean,vo);
            }else {
                return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
            }
            return JsonResult.success(vo);
        }else{
            return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
        }
    }

    @Override
    public JsonResult<List<ApartmentRoomsVO>> select(ApartmentRoomsDTO dto) {
        ApartmentRoomsExample example = new ApartmentRoomsExample();
        ApartmentRoomsExample.Criteria criteria =  example.createCriteria();
        /***
        * 根据具体的业务，查询条件赋值
         */
        List<ApartmentRooms> beanList = apartmentRoomsMapper.selectByExample(example);
        List<ApartmentRoomsVO> respDTOS = beanList.parallelStream().map((bean) -> {
            ApartmentRoomsVO vo = new ApartmentRoomsVO();
            BeanUtils.copyProperties(bean, vo);
            return vo;
        }).collect(Collectors.toList());
        return JsonResult.success(respDTOS);
    }

    @Override
    public JsonResult<PageInfo<ApartmentRoomsVO>> selectPage(ApartmentRoomsPageDTO dto) {
        ApartmentRoomsExample example = new ApartmentRoomsExample();
        ApartmentRoomsExample.Criteria criteria =  example.createCriteria();
        if(dto.getFloorId()!=null)
        {
            criteria.andFloorIdEqualTo(dto.getFloorId());
        }

        /***
        * 根据具体的业务，查询条件赋值
         */
        PageHelper.startPage(dto.getPage(), dto.getLimit());
        List<ApartmentRooms> beanList = apartmentRoomsMapper.selectByExample(example);
        PageInfo pageInfo = new PageInfo<>(beanList);
        List<ApartmentRoomsVO> respDTOS = beanList.parallelStream().map((bean) -> {
            ApartmentRoomsVO vo = new ApartmentRoomsVO();
            BeanUtils.copyProperties(bean, vo);
            return vo;
        }).collect(Collectors.toList());
        pageInfo.setList(respDTOS);
        return JsonResult.success(pageInfo);
    }

    @Override
    public JsonResult deleteById(Integer id) {
        //修改当前公寓楼层房间数量
        ApartmentFloorDTO paramsObj = new ApartmentFloorDTO();
        paramsObj.setId(id);
        paramsObj.setFloorRooms(1);
        paramsObj.setModifier("");
        paramsObj.setModifyTime(DateUtil.parseDateToStr(new Date(),"yyyy-MM-dd HH:mm:ss"));
        apartmentFloorMapper.decreaseRoomNumsById(paramsObj);

        return JsonResult.success( apartmentRoomsMapper.deleteByPrimaryKey(id));

    }

    @Override
    public JsonResult delete(ApartmentRoomsDTO dto) {
        ApartmentRoomsExample example = new ApartmentRoomsExample();
        ApartmentRoomsExample.Criteria criteria =  example.createCriteria();
        /***
        * 根据具体的业务，查询条件赋值
         */
        return JsonResult.success(apartmentRoomsMapper.deleteByExample(example));
    }
    @Override
    public JsonResult batchDelete(List<Integer> ids) {
        ApartmentRoomsExample example = new ApartmentRoomsExample();
        ApartmentRoomsExample.Criteria criteria =  example.createCriteria();
        criteria.andIdIn(ids);
        return JsonResult.success(apartmentRoomsMapper.deleteByExample(example));
    }
}